﻿using JNPF.Common.Core.Manager;
using JNPF.Common.Core.Security;
using JNPF.Common.Enum;
using JNPF.Common.Extension;
using JNPF.Common.Filter;
using JNPF.Common.Security;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.FriendlyException;
using JNPF.Website.Entitys.Dto.WebsiteContactAboutMe;
using JNPF.Website.Entitys;
using JNPF.Website.Interfaces;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace JNPF.Website;

/// <summary>
/// 业务实现：联系我们关于我们.
/// </summary>
[ApiDescriptionSettings(Tag = "Website", Name = "WebsiteContactAboutMe", Order = 200)]
[Route("api/Website/[controller]")]
public class WebsiteContactAboutMeService : IWebsiteContactAboutMeService, IDynamicApiController, ITransient
{
    /// <summary>
    /// 服务基础仓储.
    /// </summary>
    private readonly ISqlSugarRepository<WebsiteContactAboutMeEntity> _repository;

    /// <summary>
    /// 用户管理.
    /// </summary>
    private readonly IUserManager _userManager;

    /// <summary>
    /// 初始化一个<see cref="WebsiteContactAboutMeService"/>类型的新实例.
    /// </summary>
    public WebsiteContactAboutMeService(
        ISqlSugarRepository<WebsiteContactAboutMeEntity> websiteContactAboutMeRepository,
        ISqlSugarClient context,
        IUserManager userManager)
    {
        _repository = websiteContactAboutMeRepository;
        _userManager = userManager;
    }

    /// <summary>
    /// 新建联系我们关于我们.
    /// </summary>
    /// <param name="input">参数.</param>
    /// <returns></returns>
    [HttpPost("")]
    public async Task Create([FromBody] WebsiteContactAboutMeCrInput input)
    {

        var entity = (await _repository.FirstOrDefaultAsync(x => x.Id != null));
        if (entity == null)
        {
            entity=new WebsiteContactAboutMeEntity();
            entity= input.Adapt<WebsiteContactAboutMeEntity>();
            entity.Id = SnowflakeIdHelper.NextId();
            var isOk = await _repository.Context.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
            if (!(isOk > 0)) throw Oops.Oh(ErrorCode.COM1000);
        }
        else
        {
            var inputEntity = input.Adapt<WebsiteContactAboutMeEntity>();
            inputEntity.Id = entity.Id;
            var isOk = await _repository.Context.Updateable(inputEntity).ExecuteCommandAsync();
            if (!(isOk > 0)) throw Oops.Oh(ErrorCode.COM1000);
        }

    }

    /// <summary>
    /// 获取新闻管理.
    /// </summary>
    /// <param name="id">主键值.</param>
    /// <returns></returns>
    [HttpGet("")]
    public async Task<dynamic> GetInfo()
    {
        return (await _repository.FirstOrDefaultAsync(x => x.Id != null));
    }
}